package com.rocketmq.example;

import java.util.ArrayList;
import java.util.List;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

public class E11_SendFiltrationMessage {

    private static final String TOPIC = "FiltrationMessage";

    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer(TOPIC);
        producer.setNamesrvAddr(ExampleMQConfig.ENDPOINT.getName());
        try {
            producer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }

        /**
         * 批量发送消息，消息大小不能超过4MB，每条消息会产生20字节的日志开销，需要一起计算
         */
        try {
            List<Message> messageList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                Message message = new Message(TOPIC, ExampleMQConfig.TAG.getName() , ("批量消息" + i).getBytes());
                message.putUserProperty("id",  String.valueOf(i));
                messageList.add(message);
            }

            for (int i = 0; i < 10; i++) {
                Message message = new Message(TOPIC, ExampleMQConfig.TAG.getName() + i, ("批量消息" + i).getBytes());
                message.putUserProperty("id",  String.valueOf(i));
                messageList.add(message);
            }
            
            SendResult sendResult = producer.send(messageList);
            System.err.println(sendResult);

        } catch (Exception e) {
            e.printStackTrace();
        }
        producer.shutdown();
    }

}
